# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: donggu

from experimental.prophet.graph import graph


class _Instrument(object):
  def __init__(self, fastfeed_obj):
    self._fastfeed_instrument = fastfeed_obj.instrument
    self._open_interest = None
    self._funding_rate = None
    self._mark_price = None
    self._indicative_funding_rate = None

    self._volume24 = None
    self._ask_price = None
    self._bid_price = None
    self._mid_price = None
    self._fair_price = None
    self._fair_basis_rate = None
    self._last_change_percent = None
    self._last_price = None
    self._open_value = None
    self._total_volume = None
    self._total_turnover = None
    self._volume = None

  def _ensure_ready(self):
    if self._open_interest is None:
      with graph.control_if(None):
        outs = graph.create_node('fastfeed.Instrument', [self._fastfeed_instrument]).outputs

      self._open_interest = outs.open_interest
      self._funding_rate = outs.funding_rate
      self._mark_price = outs.mark_price
      self._indicative_funding_rate = outs.indicative_funding_rate

      self._volume24 = outs.volume24
      self._ask_price = outs.ask_price
      self._bid_price = outs.bid_price
      self._mid_price = outs.mid_price
      self._fair_price = outs.fair_price
      self._fair_basis_rate = outs.fair_basis_rate
      self._last_change_percent = outs.last_change_percent
      self._last_price = outs.last_price
      self._open_value = outs.open_value
      self._total_volume = outs.total_volume
      self._total_turnover = outs.total_turnover
      self._volume = outs.volume

  @property
  def open_interest(self):
    self._ensure_ready()
    return self._open_interest

  @property
  def funding_rate(self):
    self._ensure_ready()
    return self._funding_rate

  @property
  def mark_price(self):
    self._ensure_ready()
    return self._mark_price

  @property
  def indicative_funding_rate(self):
    self._ensure_ready()
    return self._indicative_funding_rate

  #############
  # Okex-only
  #############
  @property
  def volume24(self):
    self._ensure_ready()
    return self._volume24

  #############
  # Bitmex-only
  #############
  @property
  def ask_price(self):
    self._ensure_ready()
    return self._ask_price

  @property
  def bid_price(self):
    self._ensure_ready()
    return self._bid_price

  @property
  def mid_price(self):
    self._ensure_ready()
    return self._mid_price

  @property
  def fair_price(self):
    self._ensure_ready()
    return self._fair_price

  @property
  def fair_basis_rate(self):
    self._ensure_ready()
    return self._fair_basis_rate

  @property
  def last_change_percent(self):
    self._ensure_ready()
    return self._last_change_percent

  @property
  def last_price(self):
    self._ensure_ready()
    return self._last_price

  @property
  def open_value(self):
    self._ensure_ready()
    return self._open_value

  @property
  def total_volume(self):
    self._ensure_ready()
    return self._total_volume

  @property
  def total_turnover(self):
    self._ensure_ready()
    return self._total_turnover

  @property
  def volume(self):
    self._ensure_ready()
    return self._volume


def get_instrument_from_fastfeed(fastfeed_obj, **kwargs):
  cache = graph.get_aux_entry('instrument_from_fastfeed')
  key = '%s|%s' % (fastfeed_obj.instrument.instance_name, str(kwargs))
  if key not in cache:
    cache[key] = _Instrument(fastfeed_obj, **kwargs)
  return cache[key]
